package regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Test2 {
	// 元字符
	public static void test1() {
		String s = "a1b2c3d4";
		String reg = "[abc]";
		String reg1 = "[^abc]"; // 反向

		Pattern p = Pattern.compile(reg1);
		Matcher m = p.matcher(s);
		String ok = m.replaceAll("_");
		System.out.println(ok);
	}

	// 范围
	public static void test2() {
		String s = "abcdefghijklmnokprstuvwxyzAKURDZ";
		String reg = "[a-z]";
		String reg1 = "[g-k]";
		String reg2 = "[A-Z]";

		Pattern p = Pattern.compile(reg2);
		Matcher m = p.matcher(s);
		String ok = m.replaceAll("_");
		System.out.println(ok);
	}

	/*
	 * . 匹配任何字符 \d 等效于[0-9] \D等效于 [^0-9] \s 等效于空白字符，包括空格、制表符、换页符等 \S 匹配任何非空白字符 \w
	 * 等效于[a-zA-Z0-9_] 字母数字下划线 \W
	 */
	public static void test3() {
		String s = "180876";
		String reg = "[1-9]\\d\\d\\d\\d\\d"; // 6位数字密码
		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(s);

		System.out.println(m.matches());

	}

	// 边界
	/*
	 * \b 单词边界 ^ 以什么开始 $ 以什么结束
	 */
	public static void test4() {
		String s = "this is a test";
		String reg = "\\bis"; // 换 is

		String s1 = "abc is abc man abc";
		String reg1 = "^abc";
		String reg2 = "abc$";

		Pattern p = Pattern.compile(reg2);
		Matcher m = p.matcher(s1);
		String ok = m.replaceAll("_");
		System.out.println(ok);
	}

	// 量词
	/*
	 * ？出现0-1次 + 出现1-n次 出现0-n次 {n} 出现n次 {m,n} 出现m到n次 {m,} 至少出现m次
	 */
	public static void test5() {

		String s = "100089765";
		String reg = "[1-9]\\d{4,14}"; // QQ号 5到15位数字

		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(s);
		System.out.println(m.matches());
	}

	// 贪婪模式(默认) 量词后加？ 变非贪婪
	public static void test6() {

		String s = "12345678";
		String reg = "\\d{3,6}?";

		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(s);
		String ok = m.replaceAll("*");
		System.out.println(ok);
	}

	// 分组
	public static void test7() {

		String s = "a1b2c3dd";
		String reg = "([a-z]\\d){3}";

		// 把 2019-01-05 --> 05/01/2019

		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(s);
		String ok = m.replaceAll("*");
		System.out.println(ok);
	}

	// 分组2
	public static void test8() {
		// 把 2019-01-05 --> 01/05/2019
		String s = "2019-01-05";
		String reg = "(\\d{4})-(\\d{2})-(\\d{2})";

		Pattern p = Pattern.compile(reg);
		Matcher m = p.matcher(s);
		String ok = m.replaceAll("$2/$3/$1");
		System.out.println(ok);
	}
	
	// 分组3
	/*
	http://www.baidu.com/baidu.php/abc/hello.jpg
	http://b3-q.mafengwo.net/s12/M067/B2/FC/6yAAL8GKSJZzQ68.jpg
	http://b3-q.mafengwo.net/s12/M00/B2/FC/wKgED1uM1XuAUO6yAAL8GKSJZzQ68.png 
	 
	 */
		public static void test9() {
			 
			String s = "http://b4-q.mafengwo.net/s10/M00/A5/5F/wKgBZ1o3nwqAMD45ABCNqS6WqIE97.jpg"+
			"\r\nhttp://b3-q.mafengwo.net/s12/M00/B2/FC/wKgED1uM1XuAUO6yAAL8GKSJZzQ68.jpeg";
			String reg = "(http|https)://.+/(.+)";

			Pattern p = Pattern.compile(reg);
			Matcher m = p.matcher(s);
			String ok = m.replaceAll("$2");
			System.out.println(ok);
		}
	
		/*
常用正则表达式
规则	正则表达式语法  
一个或多个汉字	^[\u0391-\uFFE5]+$ 
邮政编码	^[1-9]\d{5}$
QQ号码	^[1-9]\d{4,10}$ 
邮箱	^[a-zA-Z_]{1,}[0-9]{0,}@(([a-zA-z0-9]-*){1,}\.){1,3}[a-zA-z\-]{1,}$ 
用户名（字母开头 + 数字/字母/下划线）	^[A-Za-z][A-Za-z1-9_-]+$
手机号码	^1[3|4|5|8][0-9]\d{8}$ 
URL	^((http|https)://)?([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$ 
18位身份证号	^(\d{6})(18|19|20)?(\d{2})([01]\d)([0123]\d)(\d{3})(\d|X|x)?$



*/
	

	public static void main(String[] args) {
		test9();
	}

}
